<?php
// 修改参赛信息
namespace Beerus\Modules\DesignerCompetition2024\Actions;

use Doraemon\model\ReDesignerCompetition\Sign\DcUserInfoHelper;
use Doraemon\pockets\constInterface\Designer\DesignerCompetition2024;
use Doraemon\service\Sysmsg;
use Doraemon\tools\ActionLog;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;

class EditInfo extends RowAction implements DesignerCompetition2024 {
    public $name = '修改参赛信息';

    public function handle($iInfoId, Request $request) {
        $iUid = $request->post('uid');
        $sModifyReason = $request->post('modify_reason');
        $sNick = $request->post('nick');
        $iAllowModifyStatus = (DcUserInfoHelper::getInstance(self::SIGN_YEAR))->getDataStatusAllowModify();
        $aCompetitionInfo = (DcUserInfoHelper::getInstance(self::SIGN_YEAR))->getCompetitionItemById($iInfoId);
        if ($aCompetitionInfo['data_status'] == $iAllowModifyStatus) {
            return $this->response()->error('该参赛信息正在修改中，请勿重复操作')->refresh();
        } else {
            if (empty($sModifyReason)) {
                return $this->response()->error('请填写修改理由！')->refresh();
            }
            if (!(DcUserInfoHelper::getInstance(self::SIGN_YEAR))->updateCompetition($iInfoId, ['modify_reason'=>$sModifyReason, 'data_status' => $iAllowModifyStatus])) {
                return $this->response()->error('修改参赛信息状态失败')->refresh();
            }
            // 发送下行
            $aMsgParams = [
                'msgCode' => 737,
                'to_uid' => $iUid,
                'msgParams' => [
                    'nickName' => $sNick,
                    'modifyReason' => $sModifyReason,
                ],
            ];
            Sysmsg::sendSingleMsg($aMsgParams);
            $aActionData = [
                'admin_id' => Admin::user()->id,
                'msg_params' => $aMsgParams
            ];
            ActionLog::log($iUid, $aActionData, ActionLog::DC_EDIT_COMPETITION_INFO_2022);
            return $this->response()->success('修改参赛信息下行发送成功')->refresh();
        }
    }

    public function retrieveModel(Request $request) {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form() {
        // 获取所有可选奖项
        $label = '用户uid-' . $this->row->uid .'参赛信息的修改原因';
        $this->text('modify_reason', $label)
            ->default($this->row->modify_reason)
            ->placeholder('请输入修改原因');
        // 隐藏
        $this->hidden('uid', '')->default($this->row->uid);
        $this->hidden('nick', '')->default($this->row->nick);
    }
}
